// pages/pictureBasics/pictureBasics.js
var utils = require('../../utils/utils')
var httpUtils = require('../../utils/httpUtils')
var api = require('../../utils/API')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mediaBaseUrl: httpUtils.IMAGE_URL,
        index: 0,
        currentTabKey: "0",
        height: "auto",
        topics: [
            /*{
                BasicName: "停车标志",
                ExpAudio: "/Files/ExpAudio/131816204518300406.01.mp3",
                Explain: "图一,图二，（都是代表禁止停车）图三，图四，（都是表示可以临时停车，禁止长时间停车）",
                Id: 3,
                IsDel: 0,
                JoinBy: "super",
                JoinDate: "2018-09-06T11:50:48",
                Pic1: "/Files/ExpImage/131814957762626572.846400.jpg",
                Pic2: "/Files/ExpImage/131814957762636595.865100.jpg",
                Pic3: "/Files/ExpImage/131814957762636595.846500.jpg",
                Pic4: "/Files/ExpImage/131814957762646102.865000.jpg",
                UpdateBy: "super",
                UpdateDate: "2018-09-17T09:14:11",
            },*/
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        utils.loadWindowHeight(wh => {
            utils.loadHeightById('#tabs', h => {
                that.setData({
                    height: (wh - h) + 'px',
                    tabHeight: h + 'px',
                })
            })
        })
        this.requestImageBasicData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    onSwiperChange: function (e) {
        console.log(e)
        this.setData({
            index: e.detail.current,
        })
    },

    onTabsChange: function (e) {
        console.log(e)
        let that = this;
        let current = that.data.index;
        let key = e.detail.key
        that.setData({
            currentTabKey: key
        })
        if (key === "0" || key === "4") {
            if (key === "0" && current > 0) {
                that.setData({
                    index: current - 1
                })
            } else if (key === "4" && current < that.data.topics.length - 1) {
                that.setData({
                    index: current + 1
                })
            }
        }
    },

    requestImageBasicData: function () {
        var that = this;
        httpUtils.postRequest(
            api.API_QUESTION_BASIC,
            {},
            res => {
                console.log(res)
                that.data.topics.push(res.data)
                that.setData({
                    topics: that.data.topics[0],
                    exampleCount: that.data.topics[0].length,
                })
                console.log(that.data)
            },
            fail => {
                console.log(res)
            });
    },

    /**
     * 图片加载完成回调
     * @param e
     */
    imageLoaded: function (e) {
        console.log("图片加载完成", e)
        let index = e.currentTarget.dataset.index;
        let name = e.currentTarget.dataset.name;
        let width = e.detail.width / 2 + "px";
        let height = e.detail.height / 2 + "px";

        var itemWidth = 'topics[' + index + '].' + (name + "Width");
        var itemHeight = 'topics[' + index + '].' + (name + "Height");
        this.setData({
            [itemWidth]: width,
            [itemHeight]: height,
        })
    },
})


/*
BasicName:"停车标志"
ExpAudio:"/Files/ExpAudio/131816204518300406.01.mp3"
Explain:"图一,图二，（都是代表禁止停车）图三，图四，（都是表示可以临时停车，禁止长时间停车）"
Id:3
IsDel:0
JoinBy:"super"
JoinDate:"2018-09-06T11:50:48"
Pic1:"/Files/ExpImage/131814957762626572.846400.jpg"
Pic2:"/Files/ExpImage/131814957762636595.865100.jpg"
Pic3:"/Files/ExpImage/131814957762636595.846500.jpg"
Pic4:"/Files/ExpImage/131814957762646102.865000.jpg"
UpdateBy:"super"
UpdateDate:"2018-09-17T09:14:11"
*/
